package com.wym.lottery.ui;

import com.wym.lottery.model.Bill;
import com.wym.lottery.service.BaseService;
import com.wym.lottery.util.CheckScope;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class StatisticsFragment extends Fragment {
	private static int sectionID;
	private ListView listView;
	private	EditText editText;
	private BaseService.MyBinder binder;
	private String scope;

	public static StatisticsFragment newInstance(int sectionNumber) {
		sectionID = sectionNumber;
		StatisticsFragment statisticsFragment = new StatisticsFragment();
		return statisticsFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_statistics_data, container, false);
		listView = (ListView) rootView.findViewById(R.id.lv_show_bill);
		
		editText=(EditText) rootView.findViewById(R.id.et_input_win_number);
		
		TextView tv_scope=((TextView)rootView.findViewById(R.id.tv_current_scope));
		scope=CheckScope.getScope();
		tv_scope.append(scope);
		//检查输入的开奖号码是不是4位的数字
		rootView.findViewById(R.id.btn_win_num_submit).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String s=editText.getText().toString();
				if(s.length()!=4){
					Toast.makeText(getActivity(), "输入的开奖号码不合法!", Toast.LENGTH_SHORT).show();
				editText.getText().clear();
				}
				else{
					binder=((MainActivity) getActivity()).binder;
					binder.insert(new Bill(scope,s));
					
					Cursor c=binder.getBillCursor(new Bill(scope,s));

					int[] to = new int[] { R.id.tv_time, R.id.tv_scope, R.id.tv_type, R.id.tv_sell_money, R.id.tv_pay_money };
					String[] from = new String[] { "b_time", "b_scope", "b_type_sm", "b_sell_money", "b_pay_money" };
					SimpleCursorAdapter adapter =new SimpleCursorAdapter(getActivity(), R.layout.bill_item, c, from, to,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
					listView.setAdapter(adapter);
				}
			}
		});

		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		((MainActivity) activity).onSectionAttached(sectionID);
		super.onAttach(activity);
	}
	
	private void initData(){
		
	}
}
